#include "mytype.h"
#include "stm32f10x.h"
#include "protocol.h"
#include "uart_com.h"
#include "aip1628.h"

CBuf       g_byteRxComBuf;
BYTE       g_bytebufCom[ BSIZE  ];
uint16     g_show_number=0;
uint16     g_pre_show_number=0;
uint32     g_flash_ID;
uint8      g_show_ID=0;
uint32      g_arrow_direction=11;
int        flag_wr_flash=0;
uint8      g_pc_order_done = 0;
extern unsigned char key_flag;

extern void WriteFlashOneWord(uint32_t WriteAddress,uint32_t WriteData);
extern void TIM3_Int_DeInit();

void queue_init(void)
{
	g_byteRxComBuf.Head = 0 ;
	g_byteRxComBuf.Rear = 0 ;
	g_byteRxComBuf.Length = 0 ;
	g_byteRxComBuf.n16bufSize = BSIZE ;
	g_byteRxComBuf.pbuf = g_bytebufCom;
}

BOOL queue_add_msg(pBuf MyBuf ,BYTE data )
{
  if(MyBuf->n16bufSize  == MyBuf->Length)
  {
     return FALSE ;
  }
  
  MyBuf->pbuf[MyBuf->Rear] = data ;
  
  MyBuf->Rear = ( ++MyBuf->Rear % MyBuf->n16bufSize );
  
  MyBuf->Length++ ;
  
  return TRUE ;
}

BYTE queue_get_msg(pBuf MyBuf ,uint16 Pos)
{
  if(MyBuf->Length >= Pos)
  {
    return MyBuf->pbuf[(MyBuf->Head + Pos)%MyBuf->n16bufSize];
  }
  return FALSE ;
}

BOOL queue_del_msg(pBuf MyBuf ,uint16 DelNum)
{
int i;
  if( MyBuf->Length >= DelNum)
  {	
	/*	uart_send_data(USART1,DelNum);
		uart_send_data(USART1,MyBuf->Length);
		uart_send_data(USART1,MyBuf->Head);
		for(i=0;i<DelNum;i++) {
				uart_send_data(USART1,MyBuf->pbuf[(MyBuf->Head+i )]);
		}*/
    MyBuf->Head =(MyBuf->Head + DelNum ) % MyBuf->n16bufSize ;

	MyBuf->Length -= DelNum ;
	return TRUE ;
  }
  return FALSE ;
}

BOOL DecMsg(pBuf pMyBuf, uint16 ndecodL)
{
 uint8 n8Len = 0 ;
 uint16 bytedest = 0x00 ;
 BYTE cmd = 0x00 ;
 BYTE byteVerify = 0x00 ;
 uint32 value;
 uint8 nIter = 0 ;
 BYTE DatTmp[20] = { 0 };
 
   if (pMyBuf->Length >= ndecodL ) {
    	n8Len = queue_get_msg(pMyBuf, 1) ;		 
     	if (queue_get_msg(pMyBuf,0) != 0xAA || queue_get_msg(pMyBuf,(n8Len-1) ) != 0x55 ) {
	 	     
				 queue_del_msg(pMyBuf,1);
	   	   return FALSE ;
		 }
     
		 bytedest = queue_get_msg(pMyBuf ,3) | (queue_get_msg(pMyBuf ,2)<<8);
		 /*set board addr*/
		 if (bytedest == BROADCAST_ADDR) {
			 cmd = queue_get_msg(pMyBuf,4);
			 switch (cmd) {
				 case CMD_BROADCAST_WR_ID:
					 		g_flash_ID = queue_get_msg(pMyBuf,6);
				      value = queue_get_msg(pMyBuf,5) & 0x3F;
				      g_flash_ID = ((queue_get_msg(pMyBuf,5) & 0x3F)<<8)| g_flash_ID;
			        flag_wr_flash=1;
				 			show_num(g_flash_ID);
				      value = queue_get_msg(pMyBuf,5) & 0xc0;
				      if(value == 0xc0) {
								g_arrow_direction = 10;
								g_flash_ID = (0xa << 16) | g_flash_ID;
					      show_arrow();
							} else if(value == 0x80) {
							  g_arrow_direction = 11;
								g_flash_ID = (0xb << 16) | g_flash_ID;
					      show_arrow();
							} else if(value == 0x40) {
								g_arrow_direction = 12;
								g_flash_ID = (0xc << 16) | g_flash_ID;
					      show_arrow();
							}
							break;
				 case CMD_BROADCAST_SHOW_ID:
			        g_show_ID=1;
					 break;
				 case CMD_BROADCAST_CLEAR_SHOW:
					    Tm1628_ClearDisplay();
					 break;
				 case CMD_BROADCAST_ENTER_BOOT:
					 app_deinit();
					 RCC->AHBENR |= RCC_AHBENR_CRCEN;
					 ApplicationJumpToBoot(BOOT_ADDRESS);
					 break;
				 case CMD_BROADCAST_CLEAR_HISTORY_DATA:
					 Tm1628_ClearDisplay();
					 g_show_number = 0;
				   break;
				 
				 default :
					 break;
			 }

				queue_del_msg(pMyBuf,n8Len);
			 
			  return TRUE ;
		 } else if (bytedest != g_board_ID) {  //not for this board msg del
		 		queue_del_msg(pMyBuf,n8Len);
			  return TRUE ;
		 }
	/*	 for (nIter = 0; nIter < n8Len-3; nIter++) {
	   		 byteVerify ^= queue_get_msg(pMyBuf,nIter+1);
	 	 }
			
		 if (byteVerify != queue_get_msg(pMyBuf,n8Len-2)) {
			   queue_del_msg(pMyBuf,n8Len);
	  	   return FALSE ;
		 }*/
			
		 for (nIter = 0; nIter < (n8Len-5); nIter++) {
			 DatTmp[nIter] = queue_get_msg(pMyBuf,nIter+3);
	   }
	   cmd = queue_get_msg(pMyBuf,4);
	   switch (cmd) {
		 case CMD_SHOW_NUM:
			   g_pre_show_number = 1;
		     g_show_number = queue_get_msg(pMyBuf,5);
		     g_show_number = (g_show_number<<8)| queue_get_msg(pMyBuf,6);
				 key_flag = 0;//new order ,and permission send reply to PC
		     g_pc_order_done = 0;
		     break;
		 case CMD_SHOW_REPLY_REQUST_NUM:
			   send_master_procotol(CMD_SHOW_REPLY_NUM,0);
				 break;
		 case CMD_PC_ACK_CLEAR_ORDER_DONE:
		     g_pc_order_done = 2;
		     TIM3_Int_DeInit(); //close resend 
			   break;
		 //support unicast update
		 case CMD_BROADCAST_ENTER_BOOT:
				 app_deinit();
				 RCC->AHBENR |= RCC_AHBENR_CRCEN;
				 ApplicationJumpToBoot(BOOT_ADDRESS);
				 break;
		 default:
		     break;
	
	 	}
	 	queue_del_msg(pMyBuf,n8Len);
	 	return TRUE ;
	 }
   return FALSE ;
}

void send_master_procotol(uint8 cmd,uint16 data)
{ 
	uint8 temp[10];
	uint8 i;
	
	temp[0] = PROTOCOL_HEAD;
	temp[1] = MSGSIZE;
  temp[2] = g_board_ID>>8;
	temp[3] = g_board_ID;
	temp[4] = cmd;
  temp[5] = data>>8;
	temp[6] = data;
	temp[7] = 0x00;
  temp[8] = PROTOCOL_TAIL;

	for (i=0;i<MSGSIZE;i++) {
		uart_send_data(USART2,temp[i]);
	
	}

}